﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using CE.Database;


/// <summary>
/// Summary description for ProviderUpdatesDB
/// </summary>
/// 
namespace CE.Provider
{
    public class ProviderUpdatesDB
    {
        public ProviderUpdatesDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(ProviderUpdates myProviderUpdates)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderUpdatesInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@UpdateId", myProviderUpdates.UpdateId);
            _sPara.Add("@CXTableId", myProviderUpdates.CXTableId);
            _sPara.Add("@ProviderRefId", myProviderUpdates.ProviderRefId);
            _sPara.Add("@UpdateType", myProviderUpdates.UpdateType);
            _sPara.Add("@ProviderCompanyId", myProviderUpdates.ProviderCompanyId);
            _sPara.Add("@RefCEUserId", myProviderUpdates.RefCEUserID);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public ProviderUpdates GetProviderUpdatesSingleItem(Int64 ProviderRefId, Int64 ProviderCompanyId, int CXTableId)
        {
            ProviderUpdates _myUpdates = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderUpdatesSelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderRefId", ProviderRefId);
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXTableId", CXTableId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _myUpdates = new ProviderUpdates();
                    _myUpdates = FillDataRecord(_dt, 0);
                }
            }
            return _myUpdates;
        }

        private ProviderUpdates FillDataRecord(DataTable myDataTable, int RowId)
        {
            ProviderUpdates myUpdates = new ProviderUpdates();
            myUpdates.UpdateId = Convert.ToInt64(myDataTable.Rows[RowId]["UpdateId"].ToString());
            myUpdates.CXTableId = Convert.ToInt32(myDataTable.Rows[RowId]["CXTableId"].ToString());
            myUpdates.ProviderRefId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderRefId"].ToString());
            myUpdates.UpdateType = Convert.ToInt32(myDataTable.Rows[RowId]["UpdateType"].ToString());
            myUpdates.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myUpdates.RefCEUserID = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            
            return myUpdates;
        }
    }
}
